﻿@typeparam TItem
@inherits _BaseDataGridRowEdit<TItem>
@if ( EditMode == DataGridEditMode.Form )
{
    <Validations @ref="validations" Mode="ValidationMode.Manual" StatusChanged="@ValidationsStatusChanged">
        <TableRowCell ColumnSpan="@(Columns.Count() + 1)">
            @if ( ParentDataGrid?.ShowValidationsSummary ?? true )
            {
                <Row>
                    <ValidationSummary Label="@ParentDataGrid?.ValidationsSummaryLabel" />
                </Row>
            }
            <Row>
                @foreach ( var column in Columns )
                {
                    @if ( column.ColumnType == DataGridColumnType.Command )
                        continue;

                    @if ( CellAreEditable( column ) )
                    {
                        <Column ColumnSize="ColumnSize.IsHalf.OnDesktop" Padding="Blazorise.Padding.Is2.OnX.Is2.OnY">
                            <Field>
                                <FieldLabel>
                                    @column.Caption
                                </FieldLabel>
                                <FieldBody>
                                    <_DataGridCell TItem="TItem" Column="@column" Item="@Item" CellEditContext="@CellValues[column.ElementId]" ShowValidationFeedback="@(ParentDataGrid?.ShowValidationFeedback ?? false)" />
                                </FieldBody>
                            </Field>
                        </Column>
                    }
                }
            </Row>
            <Row>
                <Column>
                    @if ( ParentDataGrid?.CommandColumn?.SaveCommandTemplate != null )
                    {
                        @ParentDataGrid.CommandColumn.SaveCommandTemplate( new CommandContext<TItem>
                        {
                            Clicked = callbackFactory.Create( this, SaveWithValidation ),
                            Item = Item,
                        } );
                    }
                    else
                    {
                        <Button Color="Color.Link" Clicked="@SaveWithValidation">Save</Button>
                    }
                    @if ( ParentDataGrid?.CommandColumn?.CancelCommandTemplate != null )
                    {
                        @ParentDataGrid.CommandColumn.CancelCommandTemplate( new CommandContext<TItem>
                        {
                            Clicked = Cancel,
                            Item = Item,
                        } );
                    }
                    else
                    {
                        <Button Color="Color.Link" Clicked="@Cancel">Cancel</Button>
                    }
                </Column>
            </Row>
        </TableRowCell>
    </Validations>
}
else if ( EditMode == DataGridEditMode.Inline )
{
    <Validations @ref="validations" Mode="ValidationMode.Manual" StatusChanged="@ValidationsStatusChanged">
        @if ( ParentDataGrid?.ShowValidationsSummary ?? true )
        {
            <TableRow Display="@(isInvalid ? Display.TableRow : Display.None)">
                <TableRowCell ColumnSpan="@Columns.Count()" Display="@(isInvalid ? Display.TableCell : Display.None)">
                    <ValidationSummary Label="@ParentDataGrid?.ValidationsSummaryLabel" />
                </TableRowCell>
            </TableRow>
        }
        @foreach ( var column in Columns )
        {
            @if ( column.ColumnType == DataGridColumnType.Command )
            {
                @if ( ParentDataGrid?.Editable == true )
                {
                    <_DataGridRowCommand TItem="TItem" Item="@Item" EditState="DataGridEditState.Edit" Save="@SaveWithValidation" Cancel="@Cancel" Width="@column.Width" />
                }
            }
            else if ( column.Displayable )
            {
                <TableRowCell width="@column.Width">
                    @if ( ParentDataGrid?.Editable == true && CellAreEditable( column ) )
                    {
                        <_DataGridCell TItem="TItem" Column="@column" Item="@Item" CellEditContext="@CellValues[column.ElementId]" ShowValidationFeedback="@( ParentDataGrid?.ShowValidationFeedback ?? false )" />
                    }
                    else
                    {
                        if ( column.DisplayTemplate != null )
                        {
                            @column.DisplayTemplate( Item )
                        }
                        else
                        {
                            @column.FormatDisplayValue( Item )
                        }
                    }
                </TableRowCell>
            }
        }
    </Validations>
}